package util.preprocessing;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;

import util.io.FileInput;

public class RankAnalysis {

	
	
	public static void number_clicks(String path, boolean hits) {

		FileInput in = new FileInput(path);

		String line = in.readString();
		int a = 1;
		int b = 3;
		Hashtable<String,Integer> rank = new Hashtable<String, Integer>();

		int c = 5;

		while (line != null) {

			String t[] = line.split("\t");
			boolean go = true;

			if (hits) {
				if (t[c].trim().equals("N")) {
					go = false;
				}

			}

			if (!(t.length > b))
				go = false;

			if (go) {
				if (rank.containsKey(t[a].trim())) {
					
					Integer i = rank.get(t[a].trim());
					rank.put(t[a].trim(),i+1);
				} else {

					LinkedList<Integer> l = new LinkedList<Integer>();

					if (t.length > b && !t[b].trim().equals(""))
					rank.put(t[a].trim(), new Integer(1));

				}
			}
			line = in.readString();

		}

		Enumeration<String> enu = rank.keys();

		while (enu.hasMoreElements()) {

			String key = enu.nextElement();

			Integer l = rank.get(key);
			if(!key.trim().equals("-"))
			System.out.println(l);

		}

	}
	
	public static void ranker(String path, boolean hits) {

			FileInput in = new FileInput(path);
	
			String line = in.readString();
			int a = 1;
			int b = 3;
		Hashtable<String, LinkedList<Integer>> rank = new Hashtable<String, LinkedList<Integer>>();

		int c = 5;

		while (line != null) {

			String t[] = line.split("\t");
			boolean go = true;
			
			
	
			if (hits) {
				if (t[c].trim().equals("N")) {
					go = false;
				}

			}
			
			if(!(t.length>b))
				go=false;

			if (go) {

				if (rank.containsKey(t[a].trim())) {

					rank.get(t[a].trim()).add(Integer.valueOf(t[b].trim()));
				} else {

					LinkedList<Integer> l = new LinkedList<Integer>();

					if (t.length > b && !t[b].trim().equals(""))

						l.add(Integer.valueOf(t[b].trim()));
					
					
					
					rank.put(t[a].trim(), l);

				}
			}
			line = in.readString();

		}

		
	
		Enumeration<String> enu = rank.keys();

		while (enu.hasMoreElements()) {

			String key = enu.nextElement();

			LinkedList<Integer> l = rank.get(key);

			double sum = 0;
			for (int i = 0; i < l.size() && !key.trim().equals("-"); i++) {

				if((double)l.get(i)!=0 )
				sum += (double)1/(double)l.get(i);
				
				
			}

			System.out.println((double)sum / (double)l.size());

		}

	}
	
	
	 public static void main(String a[]){
		 
		 String path  ="/home/sergio/projects/CODFC/queryAnalysis/aol_kids_fullQueryList.txt";
		 String p ="/home/sergio/projects/data/AOL_queryLOG/allAOL_sorted.txt";

		 number_clicks(path,true);
		 
		 
	 }
	
	
	
}
